﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace A6
{
    public class OrderDetails
    {
        public int OrderID { get; set; }
        public string OrderName { get; set; }
        public double Money { get; set; }
        public string Guest { get; set; }
        public OrderDetails(int orderID, string orderName, double money, string guest)
        {
            OrderID = orderID;
            OrderName = orderName;
            Money = money;
            Guest = guest;
        }
        public override string ToString()
        {
            return "订单号：" + OrderID.ToString() + " " + "商品名称：" + OrderName + " " + "订单金额： " + Money.ToString() + " 客户名称：" + Guest + "\n";
        }
    }
    public class Order
    {
        public OrderDetails Detail;
        public Order(OrderDetails detail)
        {
            Detail = detail;
        }
        public override string ToString()
        {
            return Detail.ToString();
        }
        public override bool Equals(Object obj)
        {
            // Performs an equality check on two points (integer pairs).
            if (obj == null || GetType() != obj.GetType()) return false;
            OrderDetails y = ((Order)obj).Detail;
            return (Detail.OrderID == y.OrderID) && (Detail.OrderName == y.OrderName) && (Detail.Money == y.Money) && (Detail.Guest == y.Guest);
        }
    }
    public class OrderService
    {
        private static MySqlConnection GetConnection()
        {
            MySqlConnection connection = new MySqlConnection(
                "datasource=localhost;username=root;" +
                "password=@Zhf0721777;database=a7;charset=utf8");
            connection.Open();
            return connection;
        }
        private List<Order> Init()
        {
            List<Order> res = new List<Order>();
            using (MySqlConnection conn = GetConnection())
            {
                string stm = "SELECT * FROM orders";
                using (MySqlCommand cmd = new MySqlCommand(stm, conn))
                {
                    using (MySqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            OrderDetails s = new OrderDetails(reader.GetInt32(0), reader.GetString(1), reader.GetInt32(2), reader.GetString(3));
                            res.Add(new Order(s));
                        }
                    }
                }
            }
            return res;
        }
        public List<Order> Orders;
        public List<Order> GetSortedOrders()
        {
            return Orders.OrderBy(o => o.Detail.OrderID).ToList();
        }

        // 支持 Lambda 自定义排序
        public List<Order> GetSortedOrders<TKey>(Func<Order, TKey> keySelector, bool ascending = true)
        {
            return ascending
                ? Orders.OrderBy(keySelector).ToList()
                : Orders.OrderByDescending(keySelector).ToList();
        }
        public OrderService()
        {
            Orders = new List<Order>();
            Orders = Init();
        }
        public void AddOrder(Order NewOrder)
        {
            foreach (var x in Orders)
            {
                if (x.Equals(NewOrder)) return;
            }
            Orders.Add(NewOrder);
            OrderDetails detail = NewOrder.Detail;
            using (MySqlConnection connection = GetConnection())
            {
                string sql = @"
            INSERT INTO orders 
            (OrderID, OrderName, Money, Guest) 
            VALUES 
            (@OrderID, @OrderName, @Money, @Guest)";

                using (MySqlCommand cmd = new MySqlCommand(sql, connection))
                {
                    // 添加参数
                    cmd.Parameters.AddWithValue("@OrderID", detail.OrderID);
                    cmd.Parameters.AddWithValue("@OrderName", detail.OrderName);
                    cmd.Parameters.AddWithValue("@Money", detail.Money);
                    cmd.Parameters.AddWithValue("@Guest", detail.Guest);
                    cmd.ExecuteNonQuery();
                }
            }
        }
        public void DeleteOrder(int ToDelete)
        {
            foreach (var x in Orders)
            {
                if (x.Detail.OrderID.Equals(ToDelete))
                {
                    Orders.Remove(x);
                    return;
                }
                
            }
            using (MySqlConnection connection = GetConnection())
            {
                string sql = "DELETE FROM orders WHERE OrderID = @orderID";
                using (MySqlCommand cmd = new MySqlCommand(sql, connection))
                {
                    // 添加参数
                    cmd.Parameters.AddWithValue("@orderID", ToDelete);
                    cmd.ExecuteNonQuery();
                }
            }
            throw new KeyNotFoundException("不存在此订单");
        }
        public void ChangeOrder(int ID, Order NewOrder)
        {
            for (int i = 0; i < Orders.Count; i++)
            {
                if (Orders[i].Detail.OrderID == ID)
                {
                    Orders[i] = NewOrder;
                    OrderDetails detail = NewOrder.Detail;
                    using (MySqlConnection connection = GetConnection())
                    {
                        string sql = "UPDATE orders SET OrderID = @OrderID , OrderName = @OrderName,Money = @Money,Guest =" +
                            " @Guest WHERE OrderID = @ID";
                        using (MySqlCommand cmd = new MySqlCommand(sql, connection))
                        {
                            cmd.Parameters.AddWithValue("@ID", ID);
                            cmd.Parameters.AddWithValue("@OrderID", detail.OrderID);
                            cmd.Parameters.AddWithValue("@OrderName", detail.OrderName);
                            cmd.Parameters.AddWithValue("@Money", detail.Money);
                            cmd.Parameters.AddWithValue("@Guest", detail.Guest);
                            cmd.ExecuteNonQuery();
                        }
                    }
                    return;
                }
            }
            throw new KeyNotFoundException("不存在此订单");
        }
        public List<Order> QueryID(int Num)
        {
            List<Order> res = new List<Order>();
            var query = Orders.
            Where(s => s.Detail.OrderID == Num)
            .OrderByDescending(s => s.Detail.Money);
            foreach (var x in query)
            {
                res.Add(x);
            }
            return res;
        }
        public List<Order> QueryName(String name)
        {
            List<Order> res = new List<Order>();
            var query = Orders.
            Where(s => s.Detail.OrderName == name)
            .OrderByDescending(s => s.Detail.Money);
            foreach (var x in query)
            {
                res.Add(x);
            }
            return res;
        }
        public List<Order> QueryMoney(double money)
        {
            List<Order> res = new List<Order>();
            var query = Orders.
            Where(s => s.Detail.Money == money)
            .OrderByDescending(s => s.Detail.Money);
            foreach (var x in query)
            {
                res.Add(x);
            }
            return res;
        }
        public List<Order> QueryGuest(string guest)
        {
            List<Order> res = new List<Order>();
            var query = Orders.
            Where(s => s.Detail.Guest == guest)
            .OrderByDescending(s => s.Detail.Money);
            foreach (var x in query)
            {
                res.Add(x);
            }
            return res;
        }
    }

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        

        static void Main()
        {
            OrderService order = new OrderService();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(order));
        }
    }
}
